package com.wsc.springbootmultidevdemo.msg.returnListener;


import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ReturnListener;
import com.wsc.springbootmultidevdemo.msg.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Sent {
    private static final String QUEUE_NAME = "return";

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();
        String exchange = "returnExc";
        String routingKey = "return.save";
        String routingKey1 = "abc.save";
        String msg = "hello rabbit return message";

        //开启不可达消息的监听
        channel.addReturnListener(new ReturnListener() {

            //param1:是否发送成功响应码 param2:未发送成功的原因 param3:交换机名称
            //param4:routingKey param5:AMQP basicProperties param6:实际消息体内容

            @Override
            public void handleReturn(int i, String s, String s1, String s2, AMQP.BasicProperties basicProperties, byte[] bytes) throws IOException {

            }
        });

        //设置 TTL 发送消息的过期时间
        AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
                .expiration("10000")
                .build();
        //Param3 : Mandatory : 如果是true,则监听器会接收到路由不可达消息，然后进行处理，如果为false，那么broker端会自动删除该消息。
        channel.basicPublish(exchange,routingKey,true,null,msg.getBytes());


    }
}
